{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>商品{:lang('list')}</legend>
    </fieldset>
    <div class="demoTable">

        <button type="button" class="layui-btn layui-btn-danger" id="exchangeall">批量兑换</button>

    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
{include file="common/foot"/}
<script type="text/html" id="action">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="exchange">{{d.status?'兑换':'已兑换'}}</a>
</script>
<script>
    layui.use('table', function() {
        var table = layui.table, $ = layui.jquery;
        var tableIn = table.render({
            id: 'message',
            elem: '#list',
            url: '{:url("exchangeindex")}',
            method: 'post',
            page: true,
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'id', title: '{:lang("id")}', width: 80,fixed: true},
                {field: 'nickname', title: '用户名(用户id)', width: 120},
                {field: 'mobile', title: '联系方式', width: 120},
                {field: 'goods', title: '商品名称', width: 120},
                {field: 'price', title: '价值', width: 120},
                {field: 'user_limit', title: '用户限量', width: 180},
                {field: 'point', title: '积分值', width: 180},
                {field: 'add_time', title: '添加时间', width: 180},
                {field: 'statusname', title: '状态', width: 180},
                {width: 200, align: 'center', toolbar: '#action'}
            ]],
            limit: 10 //每页默认显示的数量
        });
        //搜索
        $('#search').on('click', function () {
            var key = $('#key').val();
            if ($.trim(key) === '') {
                layer.msg('{:lang("pleaseEnter")}关键字！', {icon: 0});
                return;
            }
            tableIn.reload({
                where: {key: key}
            });
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'exchange') {
                layer.confirm('确认要兑换此商品吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('exchange')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
        $('#exchangeall').click(function(){
            layer.confirm('确认要兑换此商品吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('message'); //test即为参数id设定的值

                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('exchangeall')}", {ids: ids}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload();
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })
    });
</script>